// 注意：live2d_path 参数应使用绝对路径
const live2d_path = "/ytyz2020/live2d-widget/";
// 封装异步加载资源的方法
function loadExternalResource(url, type) {
    return new Promise((resolve, reject) => {
        let tag;
        if (type === "css") {
            tag = document.createElement("link");
            tag.rel = "stylesheet";
            tag.href = url;
        } else if (type === "js") {
            tag = document.createElement("script");
            tag.src = url;
        }
        if (tag) {
            tag.onload = () => resolve(url);
            tag.onerror = () => reject(url);
            document.head.appendChild(tag);
        }
    });
}
function ptime(times) {
    if (typeof dplayers != "undefined")
        for (i in dplayers)
            if (!dplayers[i].paused)
                dplayers[i].video.currentTime += times;
}
function pstime(times) {
    if (typeof dplayers != "undefined")
        for (i in dplayers)
            if (!dplayers[i].paused) {
                dplayers[i].speed(times);
                dplayers[i].notice("现在的播放速度是: " + times + "倍");
            }
}
function initjuzi() {
    fetch('https://v1.hitokoto.cn')
        .then(response => response.json())
        .then(data => {
            document.getElementById('poem_sentence').innerText = data.hitokoto;
            document.getElementById('poem_info').innerText = data.from;
        }).catch(console.error);
}
window.onload = function () {
    initjuzi();
    loadExternalResource("https://zz.bdstatic.com/linksubmit/push.js", "js");
    loadExternalResource("https://jspassport.ssl.qhimg.com/11.0.1.js?d182b3f28525f2db83acfaaf6e696dba", "js");
    if (typeof dplayers != "undefined") {
        for (i in dplayers) {
            dplayers[i].container.insertAdjacentHTML("afterEnd", '<center><div class="btn-group-sm" role="group">调整播放时间: <button type="button" class="btn btn-default" onclick="ptime(-30)">-30s</button><button type="button" class="btn btn-default" onclick="ptime(-10)">-10s</button><button type="button" class="btn btn-default" onclick="ptime(10)">+10s</button><button type="button" class="btn btn-default" onclick="ptime(30)">+30s</button></div></center><center><div class="btn-group-sm" role="group">调整播放速度: <button type="button" class="btn btn-default" onclick="pstime(1)">1(原速度)</button><button type="button" class="btn btn-default" onclick="pstime(1.1)">1.1</button><button type="button" class="btn btn-default" onclick="pstime(1.2)">1.2</button></div></center>');
        }
    }
    var live2dcontrol = localStorage.getItem("live2dcontrol");
    //var videoback = localStorage.getItem("videoback");
    localStorage.setItem("videoback", false);
    if (live2dcontrol == null) {
        live2dcontrol = false;
        localStorage.setItem("live2dcontrol", false);
    }
	/*
	if (videoback==null) {
		videoback = false;
		localStorage.setItem("videoback", false);
	}
	*/
    var model_json = localStorage.getItem("model_json");
    if (model_json == null) {
        model_json = live2d_path + "assets/Epsilon2.1/Epsilon2.1.model.json";
        localStorage.setItem("model_json", model_json);
    }
    // 加载 waifu.css live2d.min.js waifu-tips.js
    if (screen.width >= 768) {
        if (live2dcontrol == 'true') {
            Promise.all([
                loadExternalResource(live2d_path + "waifu.css", "css"),
                loadExternalResource(live2d_path + "live2d.min.js", "js"),
                loadExternalResource(live2d_path + "waifu-tips.js", "js")
            ]).then(() => {
                initWidget({
                    waifuPath: live2d_path + "waifu-tips.json",
                    apiPath: "https://live2d.fghrsh.net/api/",
                    //apiPath: " https://server.whitemagic2014.com:8443/live2d",
                    //cdnPath: "https://hhhh/live2d_api/"
                    //apiPath: live2d_path + "modellist/",
                });
            });
        }
		/*
		if(videoback=='true'){
			//document.body.innerHTML+='<div id="vcontainer" class="vcontainer"><span id="vloading" class="vloading" data-percent="0"></span></div>';
			loadExternalResource("/ytyz2020/assets/css/video.css", "css");
			loadExternalResource("/ytyz2020/assets/js/video.js", "js");
			document.getElementById('board').style.backgroundColor='transparent';
		}
		*/
    } else if (screen.width >= 350 && screen.width < 768) {
        if (live2dcontrol == 'true') {
            Promise.all([
                loadExternalResource(live2d_path + "lib/L2Dwidget.min.js", "js"),
                //loadExternalResource(live2d_path + "waifu2.css", "css"),
                //loadExternalResource(live2d_path + "waifu-tips2.js", "js")
            ]).then(() => {
                L2Dwidget.init({
                    "model": {
                        "jsonPath": model_json
                    },
                    "mobile": {
                        "show": true
                    },
                    "display": {
                        "vOffset": -55,
                        "hOffset": 0
                    },
                    dialog: {
                        // 开启对话框
                        enable: localStorage.getItem("live2ddialog") == 'true' ? true : false,
                        script: {
                            // 每空闲 10 秒钟，显示一条一言
                            'every idle 15s': '$hitokoto$',
                            // 当触摸到角色身体
                            'tap body': '哎呀！别碰我！',
                            // 当触摸到角色头部
                            'tap face': '人家已经不是小孩子了！'
                        }
                    }
                });
                //loadWidget();
            });
        }
    }
    var r = confirm("随着时间流逝，蚊香直播提供的地址已部分失效，我保存了大部分，遗憾的是不能保住全部\n点击确定跳转到哔哩哔哩播放页，否则继续浏览本站\n为什么一开始没有使用哔哩哔哩呢？\n因为审核需要一段时间，请同学们谅解")
    if (r == true) {
        window.location.href = "https://www.bilibili.com/video/BV1Vi4y1t7XT"
    } 
}
